﻿using _1._13._2015_Art.Data.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Migrations;

namespace _1._13._2015_Art.Data
{
    public static class Seeder
    {
        public static void Seed(ApplicationDbContext db)
        {

            // Create roles, and an admin
            UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(db);
            UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(userStore);

            RoleStore<Role> roleStore = new RoleStore<Role>(db);
            RoleManager<Role> roleManager = new RoleManager<Role>(roleStore);

            if (!roleManager.RoleExists("Artist"))
            {
                roleManager.Create(new Role
                {
                    Name = "Artist"
                });
            }
            if (!roleManager.RoleExists("Admin"))
            {
                roleManager.Create(new Role
                {
                    Name = "Admin"
                });
            }

            // first add a manager
            ApplicationUser louis = userManager.FindByName("louism817@gmail.com");

            if (louis == null)
            {
                louis = new ApplicationUser
                {
                    FirstName = "Louis",
                    LastName = "Murphy",
                    Bio = "Louis likes long walks on the beach.",
                    Email = "louism817@gmail.com",
                    UserName = "louism817@gmail.com"
                };
                userManager.Create(louis, "123456");
                userManager.AddToRole(louis.Id, "Admin");
            }

            // now add some artists
            ApplicationUser ima = userManager.FindByName("ima@mail.com");

            if (ima == null)
            {
                ima = new ApplicationUser
                {
                    FirstName = "Ima",
                    LastName = "Painter",
                    Bio = "Ima has been paining as long as she can remember.",
                    Email = "ima@mail.com",
                    UserName = "ima@mail.com"
                };
                userManager.Create(ima, "123456");
                userManager.AddToRole(ima.Id, "Artist");
            }

            ApplicationUser hesa = userManager.FindByName("hesa@mail.com");

            if (hesa == null)
            {
                hesa = new ApplicationUser
                {
                    FirstName = "Hesa",
                    LastName = "Sculpter",
                    Bio = "Hesa is a fantastic sculpture, using mainly macaroni as a medium.",
                    Email = "hesa@mail.com",
                    UserName = "hesa@mail.com"
                };
                userManager.Create(hesa, "123456");
                userManager.AddToRole(ima.Id, "Artist");
            }                                      

            db.SaveChanges();
            
        }
    }
}
